1 //-------------------------------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // Ref Counted Pointer.
7 // Only safe for single threaded operations
9 //-------------------------------------------------------------------------------------------------
36 SafeInt
<unsigned int> Count
;
47 explicit RefCountedPtr(T
* pData
)
50 m_pData
->pData
= pData
;
54 RefCountedPtr(const RefCountedPtr
<T
> &other
)
57 // Increment count before assigning the pointer. Pointer assignment is
58 // exception safe but incrementing the count is not. If incrementing the
59 // count overflows then the count would be N and the number of references
60 // would be N+1 resulting in a double free.
63 other
.m_pData
->Count
+=1;
64 m_pData
= other
.m_pData
;
75 return m_pData
? m_pData
->pData
: NULL
;
80 VSASSERT(m_pData
, "Accessing NULL Pointer");
81 return *(m_pData
->pData
);
86 VSASSERT(m_pData
, "Accessing NULL Pointer");
87 return m_pData
->pData
;
90 const T
* operator ->() const
92 VSASSERT(m_pData
, "Accessing NULL Pointer");
93 return m_pData
->pData
;
96 const RefCountedPtr
<T
>& operator=(const RefCountedPtr
<T
>& other
)
98 RefCountedPtr
<T
> temp(other
);
99 TemplateUtil::Swap(&m_pData
, &temp
.m_pData
);
103 const RefCountedPtr
<T
>& operator=(const T
& other
)
105 RefCountedPtr
<T
> temp(new T(other
));
106 TemplateUtil::Swap(&m_pData
, &temp
.m_pData
);
110 const RefCountedPtr
<T
>& operator=(T
* ptr
)
112 RefCountedPtr
<T
> temp(ptr
);
113 TemplateUtil::Swap(&m_pData
, &temp
.m_pData
);
117 void Attach( _In_ T
*pData
)
120 RefCountedPtr
<T
> temp(pData
);
128 VSASSERT(m_pData
->Count
.Value() != 0, "RefCountedPtr count error");
131 if ( 0 == m_pData
->Count
.Value() )
141 return (m_pData
) ? m_pData
->pData
: NULL
;
146 void RemoveReference()